<?php
/**
 * Created by PhpStorm.
 * User: gakuin
 * Date: 7/10/16
 * Time: 10:55 PM
 */

namespace app\common;

date_default_timezone_set("Asia/Shanghai");

class CommonHelper
{
    public static function getTodayDate()
    {
        return date("Y-m-d",time());
    }

    public static function getTodayDateMore()
    {
        return date("Y-m-d H:i:s",time());
    }
    public static function getYesterdayDate()
    {
        return date("Y-m-d",strtotime("-1 day"));
    }

    public static function getTomorrowDateHI()
    {
        $yesterdayDateHI = date("Y-m-d",strtotime("+1 day"))." 00:00";
        return $yesterdayDateHI;
    }

    public static function getTodayDateHI(){
        $todayDateHI = date("Y-m-d",time())." 12:00";
        return $todayDateHI;
    }
    public static function getYesterdayDateHI()
    {
        $yesterdayDateHI = date("Y-m-d",strtotime("-1 day"))." 00:00";
        return $yesterdayDateHI;
    }

    public static function getTwoDayAgoDateHI()
    {
        $twoDayAgoDateHI = date("Y-m-d",strtotime("-2 day"))." 00:00";
        return $twoDayAgoDateHI;
    }

    public static function getTwentyAgoDateHI()
    {
        $twentyAgoDateHI = date("Y-m-d",strtotime("-31 day"))." 00:00";
        return $twentyAgoDateHI;
    }

    public static function getIp()
    {

//        return 'https://api.hoooty.com/';//正式服务器
        return 'https://apibeta.hoooty.com/';//测试服务器
    }

    public static function getEaseAppKey(){
//        return '15805797361#hotcityofficial';//环信正式
        return '15805797361#hotcitybeta';  //测试服务器环信key
    }

    public static function getEaseIdPre(){
//        return 'oau';  //正式服务器环信id前缀
        return 'bau';  //测试服务器环信id前缀
    }

    //替换指定的敏感词
    public static function  replace_sensitive_word(){
        $arr =  file_get_contents("../web/js/sensitive_word.txt");//敏感词典
        $array = explode("\r\n", $arr);
        return  $array;
    }

    //根据积分转换vip
    public static function getUserLevel($integral){
        if (0 <= $integral && $integral < 50000) {
            return 0;
        }
        if (50000 <= $integral && $integral < 150000) {
            return 1;
        }
        if (150000 <= $integral && $integral < 300000) {
            return 2;
        }
        if (300000 <= $integral && $integral < 500000) {
            return 3;
        }
        if (500000 <= $integral && $integral < 800000) {
            return 4;
        }
        if (800000 <= $integral && $integral < 1200000) {
            return 5;
        }
        if (1200000 <= $integral && $integral < 2000000) {
            return 6;
        }
        if (2000000 <= $integral && $integral < 3000000) {
            return 7;
        }
        if (3000000 <= $integral && $integral < 5000000) {
            return 8;
        }
        if (5000000 <= $integral && $integral < 10000000) {
            return 9;
        }
        if (10000000 <= $integral) {
            return 10;
        }

        return 99;
    }

    //获取指定年份月份天数
    public static function get_day($year,$month){

        if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 10 , 12)))
        {
            $text = 31;
        }
        else if( $month == 2 )
        {
            if ( $year%400 == 0  || ($year%4 == 0 && $year%100 !== 0) )        //判断是否是闰年
            {
                $text = 29;
            }
            else{

                $text = 28;
            }
        }
        else{

            $text = 30;
        }
        return $text;
    }

    //获取指定时间前一天日期
    public static function get_day_before($date){
        return date("Y-m-d H:i:s",(strtotime($date) - 3600*24));
    }

    //日期格式修改
    public static function format_day($year,$month,$day){

        $date = $year;
        if($month > 9){
            $date = $date."-".$month;
        }else{
            $date = $date."-0".$month;
        }

        if($day > 9){
            $date = $date."-".$day;
        }else{
            $date = $date."-0".$day;
        }

        return $date." 12:00:00";
    }

    //生成唯一id
    public static function create_uuid(){    //可以指定前缀
        $str = md5(uniqid(mt_rand(), true));

        return $str;
    }

    //根据url请求数据
    public static function getCurl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close ($ch);
        return $result;
    }

    //抽奖环节 奖品
    public static function getCouponByCount(){

        $index = rand(0,100);//中奖物品通过一个随机数生成
        $type = -1;
        $screen_delay = -1;

        if($index>=0 && $index<9){
            $index = 0;//10
        }else if($index>=9 && $index<19){
            $index = 2;//30
        }else if($index>=19 && $index<29){
            $index = 4;//10
        }else if($index>=29 && $index<34){
            $index = 3;//52
        }else if($index>=34 && $index<90){
            $index = 5;//没中奖
        }else if($index>=90 && $index<100){
            $index = 6;//20
        }else if($index==100){
            $index = 7;//60
        }

        switch ($index){
            case 0:
                $type = 16802;
                $screen_delay = 10;
                break;

            case 1:
                $type = 16802;
                $screen_delay = 1314;
                break;

            case 2:
                $type = 16802;
                $screen_delay = 20;
                break;

            case 3:
                $type = 16802;
                $screen_delay = 52;
                break;

            case 4:
                $type = 16801;
                $screen_delay = 10;
                break;

            case 5:
                $type = -1;
                $screen_delay = -1;
                break;

            case 6:
                $type = 16801;
                $screen_delay = 30;
                break;

            case 7:
                $type = 16802;
                $screen_delay = 60;
                break;
        }

        return ['prize_index'=>$index,'type'=>$type,'screen_delay'=>$screen_delay];
    }

    //获取本周所有日期
    function getDaysInWeek(){
        $days=array();
        $date = date("Y-m-d"); //当前日期
        $first=1; //$first =1 表示每周星期一为开始时间 0表示每周日为开始时间
        $w = date("w", strtotime($date)); //获取当前周的第几天 周日是 0 周一 到周六是 1 -6
        $d = $w ? $w - $first : 6; //如果是周日 -6天
        $now_start = date("Ymd", strtotime("$date -".$d." days")); //本周开始时间
        array_push($days,$now_start);
        $now_1 = date("Ymd", strtotime("$now_start +1 days")); //本周第二天
        array_push($days,$now_1);
        $now_2 = date("Ymd", strtotime("$now_start +2 days")); //本周第三天
        array_push($days,$now_2);
        $now_3 = date("Ymd", strtotime("$now_start +3 days")); //本周第四天
        array_push($days,$now_3);
        $now_4 = date("Ymd", strtotime("$now_start +4 days")); //本周第五天
        array_push($days,$now_4);
        $now_5 = date("Ymd", strtotime("$now_start +5 days")); //本周第六天
        array_push($days,$now_5);
        $now_end = date("Ymd", strtotime("$now_start +6 days")); //本周结束时间
        array_push($days,$now_end);

        return $days;
    }

    //获取年份+周数
    function get_weeks_num($time){
        $year = date('Y');
        $month = intval(date('m',$time));//当前时间的月份
        $fyear = strtotime(date('Y-01-01',$time));//今年第一天时间戳
        $fdate = intval(date('N',$fyear));//今年第一天 周几
        $sysweek = intval(date('W',$time));//系统时间的第几周
        //大于等于52 且 当前月为1时， 返回1
        if(($sysweek >= 52 && $month == 1)){
            return $year."1";
        }elseif($fdate == 1){
            //如果今年的第一天是周一,返回系统时间第几周
            return $year.$sysweek;
        }else{
            //返回系统周+1
            return $year.($sysweek + 1);
        }
    }
}